< previous page page_288 next page >

Page 288
0288-01.jpg
Figure 8.20.
Using the 
DISTINCT keyword to
remove duplicates from a result set.
If you include more than one column in the SELECT list, all columns are used to evaluate the uniqueness of the row. Execute and compare the result sets of the following two SQL statements. Refer to Figure 8.21 as a guide.
SELECT DISTINCT Title
   FROM BookSales
SELECT DISTINCT Title, Units
   FROM BookSales
0288-02.jpg
Figure 8.21.
Using 
DISTINCT on multiple columns.
Notice that the first SQL statement returns a single record for each Title in the data table. The second SQL statement returns more records for each Title because there are distinct Units values for each Title.
There are also times when you want to collect data on all rows that are distinct in any of the fields. Instead of using the DISTINCT keyword and listing all the fields in the table, you can

 
< previous page page_288 next page >

If you like this book, buy it!